package com.bbx.taxi.client.Fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.baidu.location.BDLocation;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.passanger.Return.Elements;
import com.bbx.api.sdk.model.passanger.Return.OrderId;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.End;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Location;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Locations;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Start;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.CodeNet;
import com.bbx.api.sdk.net.passeger.conn.OrderSubmitNet;
import com.bbx.api.util.LogUtils;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.ContactPersonActivity;
import com.bbx.taxi.client.Activity.LeaveMessageActivity;
import com.bbx.taxi.client.Activity.PlaneActivity;
import com.bbx.taxi.client.Activity.MainActivity;
import com.bbx.taxi.client.Activity.MainActivity.MyOnPageScrolledListener;
import com.bbx.taxi.client.Activity.MainActivity.OnMenuClickListener;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Activity.login.LoginActivity.CodeTask;
import com.bbx.taxi.client.Activity.Address.EditAddressActivity;
import com.bbx.taxi.client.Activity.Base.BaseFragment;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.AddressMsg;
import com.bbx.taxi.client.Bean.Extra.ContactPersonMsg;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.client.MyApplication.OnLocationResultListener;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Util.ClickFilter;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.widget.pop.CollecteAddressPop;
import com.bbx.taxi.client.widget.pop.CollecteAddressPop.OnAddressItemClickListener;
import com.bbx.taxi.client.widget.pop.DatePicker;
import com.bbx.taxi.client.widget.pop.DatePicker.OnConfirmListener;
import com.bbx.taxi.client.widget.pop.DatePicker.OnDateConfirmListener;
import com.bbx.taxi.client.widget.pop.DatePicker.OnDateScrollResultListener;
import com.bbx.taxi.client.widget.pop.DatePicker.OnPeopleScrollResultListener;
import com.example.mydatepicker.wheelview.WheelView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 拼车出行
 */
public class CarpoolingFragment extends BaseFragment implements
		OnClickListener, OnMenuClickListener,MyOnPageScrolledListener {
	private RelativeLayout timelayout;
	private RelativeLayout personlayout;
	private RelativeLayout layout_plane;
	private RelativeLayout layout_convert;
	private RelativeLayout layout_people;
	private RelativeLayout layout_start_address;
	private RelativeLayout layout_end_address;
	private RelativeLayout layout_message;
	private LinearLayout layout_start_collecte, layout_end_collecte;
	private TextView tv_time;
	private TextView tv_plane_numble;
	private TextView tv_plane_time;
	private TextView tv_start_address;
	private TextView tv_end_address;
	public TextView tv_telephone;
	private TextView tv_people_number;
	private TextView tv_message;
	private ImageView iv__plane_delete;
	private Context mContext;
	private MyApplication mApplication;
	/** 主界面的信息临时存储 */
	private MainAttribute main;
	private MainActivity mainActivity;

	public void onCreateView(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_carpooling);
		mApplication = MyApplication.getInstance();
		mContext = mApplication.getApplicationContext();
		main = MainAttribute.getInstance();
		 mainActivity=(MainActivity)getActivity();
		 mainActivity.setMyOnPageScrolledListener(this);
		init();
		initView();
	}

	private void init() {
		tv_time = (TextView) findViewById(R.id.tv_time);
		tv_plane_numble = (TextView) findViewById(R.id.tv_plane_numble);
		tv_plane_time = (TextView) findViewById(R.id.tv_plane_time);
		tv_start_address = (TextView) findViewById(R.id.tv_start_address);
		tv_end_address = (TextView) findViewById(R.id.tv_end_address);
		tv_telephone = (TextView) findViewById(R.id.tv_telephone);
		tv_people_number = (TextView) findViewById(R.id.tv_people_number);
		tv_message = (TextView) findViewById(R.id.tv_message);
		timelayout = (RelativeLayout) findViewById(R.id.timelayout);
		layout_plane = (RelativeLayout) findViewById(R.id.layout_plane);
		personlayout = (RelativeLayout) findViewById(R.id.personlayout);
		layout_convert = (RelativeLayout) findViewById(R.id.layout_convert);
		layout_people = (RelativeLayout) findViewById(R.id.layout_people);
		layout_start_address = (RelativeLayout) findViewById(R.id.layout_start_address);
		layout_end_address = (RelativeLayout) findViewById(R.id.layout_end_address);
		layout_message = (RelativeLayout) findViewById(R.id.layout_message);
		layout_start_collecte = (LinearLayout) findViewById(R.id.layout_start_collecte);
		layout_end_collecte = (LinearLayout) findViewById(R.id.layout_end_collecte);
		iv__plane_delete = (ImageView) findViewById(R.id.iv__plane_delete);
		timelayout.setOnClickListener(this);
		personlayout.setOnClickListener(this);
		layout_convert.setOnClickListener(this);
		layout_people.setOnClickListener(this);
		layout_start_address.setOnClickListener(this);
		layout_end_address.setOnClickListener(this);
		iv__plane_delete.setOnClickListener(this);
		layout_message.setOnClickListener(this);
		layout_start_collecte.setOnClickListener(this);
		layout_end_collecte.setOnClickListener(this);
		MainActivity.setOnMenuClickListener(this);
	}

	public void initView() {
		main = MainAttribute.getInstance();
		mainActivity=(MainActivity)getActivity();
		if (main != null&&tv_time!=null) {
			// 时间
			tv_time.setText(main.getTime());
			// 出发地
			if (main.getStartName() != null) {
				tv_start_address.setText(main.getStartName());
			}
			// 目的地
			if (main.getEndName() != null) {
				tv_end_address.setText(main.getEndName());
			}
			// 姓名和电话
			if (main.getName() != null && main.getTel() != null
					&& !main.getTel().equals("")) {
				tv_telephone.setText((main.getName().equals("") ? mContext
						.getString(R.string.i) : main.getName())
						+ "-"
						+ FormatUtil.onFormatTelephone(main.getTel()));
			}
			// 人数
			tv_people_number.setText(String.format(
					mContext.getString(R.string.num), main.getCount()));
			// 留言
			if (main.getMessage_PC() != null) {
				tv_message.setText(main.getMessage_PC());
			} else {
				tv_message.setText("");
			}
			// 有航班，取消點擊
			if (main.getPlaneStatus() != 0) {
				layout_end_address.setClickable(false);
				timelayout.setClickable(false);
				layout_plane.setVisibility(View.VISIBLE);
				tv_plane_numble.setText(main.getPlaneNumble());
				tv_plane_time.setText(main.getTime());
			} else {
				layout_plane.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onClick(View v) {
		if (!ClickFilter.filter())
			return;
		switch (v.getId()) {
		case R.id.timelayout:
			// DatePicker.getInstance(mContext).onShow(v, 1);
			// DatePicker.getInstance(mContext).setOnDateScrollResultListener(
			// new OnDateScrollResultListener() {
			// @Override
			// public void OnDateScrollResult(List<String> list,
			// WheelView wl_day, WheelView wl_hour, WheelView wl_min) {
			// switch (wl_day.getCurrentItem()) {
			// case 0:// 接班机
			// wl_hour.setCurrentItem(0, true);
			// wl_min.setCurrentItem(0, true);
			// break;
			// case 1://
			// wl_hour.setCurrentItem(1, true);
			// wl_min.setCurrentItem(1, true);
			// break;
			// case 2:// 今天，滑动到当前时间
			// long Time =
			// DateFormat.getTime()+20*60*1000;//加20分上去，是为了不让选择当前时间+20分以下的时间
			// Calendar mCalendar = Calendar.getInstance();
			// mCalendar.setTimeInMillis(Time);
			// int hour=mCalendar.get(Calendar.HOUR_OF_DAY);
			// int minute=mCalendar.get(Calendar.MINUTE);
			// if((wl_hour.getCurrentItem()-2)<=(hour)){
			// wl_hour.setCurrentItem(hour+2,true);
			// if((wl_min.getCurrentItem()-2)<=(minute)){
			// wl_min.setCurrentItem(minute+2,true);
			// }
			// }
			// break;
			// default:
			// if (wl_hour.getCurrentItem() == 0
			// || wl_hour.getCurrentItem() == 1) {
			// wl_hour.setCurrentItem(2, true);
			// }
			// if (wl_min.getCurrentItem() == 0
			// || wl_min.getCurrentItem() == 1) {
			// wl_min.setCurrentItem(2, true);
			// }
			// break;
			// }
			// }
			// });
			// DatePicker.getInstance(mContext).setOnDateConfirmListener(
			// new OnDateConfirmListener() {
			// @Override
			// public void onDateConfirm(List<String> list,
			// WheelView wl_day, WheelView wl_hour, WheelView wl_min) {
			// if(wl_day.getCurrentItem()==0||wl_day.getCurrentItem()==1){
			// main.setTime(list.get(wl_day.getCurrentItem()));
			// tv_time.setText(main.getTime());
			// if(wl_day.getCurrentItem()==0){
			// startActivityForResult(new Intent(mContext,PlaneActivity.class),
			// MainAttribute.REQUEST_CODE_PLANE);
			// }
			// return ;
			// }
			// main.setTime(DatePicker.getInstance(mContext).onTimeFormat(list.get(wl_day.getCurrentItem()),
			// wl_hour.getCurrentItem() - 2, wl_min.getCurrentItem() - 2));
			// tv_time.setText(main.getTime());
			// }
			// });
			DatePicker.getInstance(mContext).onShow(v, 2);
			DatePicker.getInstance(mContext).setOnDateScrollResultListener(
					new OnDateScrollResultListener() {
						@Override
						public void OnDateScrollResult(List<String> list,List<String> list_min,
								WheelView wl_day, WheelView wl_hour,
								WheelView wl_min) {
							switch (wl_day.getCurrentItem()) {
							case 0://
								wl_hour.setCurrentItem(0, true);
								wl_min.setCurrentItem(0, true);
								break;
							case 1:// 今天，滑动到当前时间
								long Time = DateFormat.getTime() + 20 * 60 * 1000;// 加20分上去，是为了不让选择当前时间+20分以下的时间
								Calendar mCalendar = Calendar.getInstance();
								mCalendar.setTimeInMillis(Time);
								int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
								int minute = mCalendar.get(Calendar.MINUTE);
								if ((wl_hour.getCurrentItem() - 1) <= (hour)) {
									wl_hour.setCurrentItem(hour + 1, true);
									if (wl_min.getCurrentItem()==0||Integer.parseInt(list_min.get(wl_min.getCurrentItem() )) <= (minute)) {
										int index=0;
										for(int i=1;i<list_min.size();i++){
											int min=Integer.parseInt(list_min.get(i));
											if(minute==min){
												index=i;
												break;
											}else{
												if(minute>min&&(minute-min<5)){
													index=i+1;
													break;
												}
											}
										}
										wl_min.setCurrentItem(index, true);
									}
								}
								break;
							default:
								if (wl_hour.getCurrentItem() == 0) {
									wl_hour.setCurrentItem(1, true);
								}
								if (wl_min.getCurrentItem() == 0) {
									wl_min.setCurrentItem(1, true);
								}
								break;
							}
						}
					});
			DatePicker.getInstance(mContext).setOnDateConfirmListener(
					new OnDateConfirmListener() {
						@Override
						public void onDateConfirm(List<String> list,List<String> list_min,
								WheelView wl_day, WheelView wl_hour,
								WheelView wl_min,int estimate_time) {
							if (wl_day.getCurrentItem() == 0) {
								main.setTime(list.get(wl_day.getCurrentItem()));
								tv_time.setText(main.getTime());
								return;
							}
							main.setTime(DatePicker.getInstance(mContext)
									.onTimeFormat(
											list.get(wl_day.getCurrentItem()),
											wl_hour.getCurrentItem() - 1,
											Integer.parseInt(list_min.get(wl_min.getCurrentItem() ))));
							tv_time.setText(main.getTime());
						}
					});
			break;
		case R.id.layout_people:
			DatePicker.getInstance(mContext).onShowPeople(v, 4,
					mContext.getString(R.string.num_rule));
			DatePicker.getInstance(mContext).setOnConfirmListener(
					new OnConfirmListener() {
						@Override
						public void onConfirm(List<String> list,
								WheelView people) {
							main.setCount(people.getCurrentItem() + 1);
							tv_people_number.setText(String.format(
									mContext.getString(R.string.num),
									main.getCount()));
						}
					});
			break;
		case R.id.personlayout:
			personlayout.setClickable(false);
			startActivityForResult(
					new Intent(mContext, ContactPersonActivity.class)
							.putExtra(ContactPersonMsg.extra_contact_name,
									main.getName()).putExtra(
									ContactPersonMsg.extra_contact_phone,
									main.getTel()),
					MainAttribute.REQUEST_CODE_PERSON);
			break;
		case R.id.layout_start_address:
			layout_start_address.setClickable(false);
			startActivityForResult(
					new Intent(mContext, EditAddressActivity.class).putExtra(
							AddressMsg.extra_editaddress,
							AddressMsg.ADDRESS_START_MAIN).putExtra(
							AddressMsg.extra_cityopend,mainActivity.getCityNotOpened(false)),
					MainAttribute.REQUEST_CODE_START_ADDRESS);
			break;
		case R.id.layout_end_address:
			layout_end_address.setClickable(false);
			startActivityForResult(new Intent(mContext,
					EditAddressActivity.class).putExtra(
					AddressMsg.extra_editaddress, AddressMsg.ADDRESS_END_MAIN),
					MainAttribute.REQUEST_CODE_END_ADDRESS);
			break;
		case R.id.layout_convert:
			main.onSwap();
			tv_start_address.setText(main.getStartName());
			tv_end_address.setText(main.getEndName());
			tv_end_address.setTextColor(mContext.getResources().getColor(
					R.color.black));
			break;
		case R.id.iv__plane_delete:
			layout_plane.setVisibility(View.GONE);
			layout_end_address.setClickable(true);
			timelayout.setClickable(true);
			main.setPlaneStatus(0);
			break;
		case R.id.layout_message:
			startActivityForResult(new Intent(mContext,
					LeaveMessageActivity.class).putExtra(MainMsg.extra_message,
					MainMsg.MESSAGE_MAIN_PC),
					MainAttribute.REQUEST_CODE_MESSAGE);
			break;
		case R.id.layout_start_collecte:
			CollecteAddressPop.getInstance(mContext).onShow(v);
			CollecteAddressPop.getInstance(mContext).setOnItemClickListener(
					new OnAddressItemClickListener() {
						@Override
						public void onItemClick(List<Map<String, String>> list,
								int position) {
							main.setStartName(list.get(position).get(
									DBcolumns.ADDRESS_NAME));
							main.setStartCity(list.get(position).get(
									DBcolumns.ADDRESS_CITY));
							main.setStartAddress(list.get(position).get(
									DBcolumns.ADDRESS_ADDRESS));
							main.setStartLongitude(Double.parseDouble(list.get(
									position).get(DBcolumns.ADDRESS_LONGITUDE)));
							main.setStartLatitude(Double.parseDouble(list.get(
									position).get(DBcolumns.ADDRESS_LATITUDE)));
							if (!main.getStartName().equals("")) {
								tv_start_address.setText(main.getStartName());
							}
						}
					});
			break;
		case R.id.layout_end_collecte:
			CollecteAddressPop.getInstance(mContext).onShow(v);
			CollecteAddressPop.getInstance(mContext).setOnItemClickListener(
					new OnAddressItemClickListener() {
						@Override
						public void onItemClick(List<Map<String, String>> list,
								int position) {
							main.setEndName(list.get(position).get(
									DBcolumns.ADDRESS_NAME));
							main.setEndCity(list.get(position).get(
									DBcolumns.ADDRESS_CITY));
							main.setEndAddress(list.get(position).get(
									DBcolumns.ADDRESS_ADDRESS));
							main.setEndLongitude(Double.parseDouble(list.get(
									position).get(DBcolumns.ADDRESS_LONGITUDE)));
							main.setEndLatitude(Double.parseDouble(list.get(
									position).get(DBcolumns.ADDRESS_LATITUDE)));
							if (!main.getEndName().equals("")) {
								tv_end_address.setText(main.getEndName());
							}
						}
					});
			break;

		}
	}

	public boolean onLocationResult() {
		boolean isError = false;
		if (tv_start_address != null) {
			tv_start_address.setText(main.getStartName());
		} else {
			isError = true;
		}
		return isError;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case MainAttribute.REQUEST_CODE_PERSON:// 电话
			personlayout.setClickable(true);
			if (data != null) {
				main.setTel(data.getStringExtra("telephone"));
				main.setName(data.getStringExtra("name"));
				if (main.getName() != null && main.getTel() != null
						&& !main.getName().equals("")
						&& !main.getTel().equals("")) {
					tv_telephone.setText(main.getName() + "-"
							+ FormatUtil.onFormatTelephone(main.getTel()));
				}
			}
			break;
		case MainAttribute.REQUEST_CODE_START_ADDRESS:// 出发地
			layout_start_address.setClickable(true);
			if (data != null) {
				main.setStartCity(data
						.getStringExtra(AddressMsg.extra_address_city));
				main.setStartAddress(data
						.getStringExtra(AddressMsg.extra_address_address));
				main.setStartLongitude(data.getDoubleExtra(
						AddressMsg.extra_address_longitude, 0));
				main.setStartLatitude(data.getDoubleExtra(
						AddressMsg.extra_address_latitude, 0));
				main.setStartName(data
						.getStringExtra(AddressMsg.extra_address_name));
				if (!main.getStartName().equals("")) {
					tv_start_address.setText(main.getStartName());
				}
			}
			break;
		case MainAttribute.REQUEST_CODE_END_ADDRESS:
			layout_end_address.setClickable(true);
			if (data != null) {

				main.setEndCity(data
						.getStringExtra(AddressMsg.extra_address_city));
				main.setEndAddress(data
						.getStringExtra(AddressMsg.extra_address_address));
				main.setEndLongitude(data.getDoubleExtra(
						AddressMsg.extra_address_longitude, 0));
				main.setEndLatitude(data.getDoubleExtra(
						AddressMsg.extra_address_latitude, 0));
				main.setEndName(data
						.getStringExtra(AddressMsg.extra_address_name));
				if (!main.getEndName().equals("")) {
					tv_end_address.setText(main.getEndName());
					tv_end_address.setTextColor(mContext.getResources()
							.getColor(R.color.black));
				}
				LogUtils.e("", data.getStringExtra(AddressMsg.extra_address_address));
			}
			break;
		case MainAttribute.REQUEST_CODE_PLANE:
			if (data != null) {
				layout_plane.setVisibility(View.VISIBLE);
				main.setPlaneStatus(data.getIntExtra("plane_status", 0));
				main.setTime(data.getStringExtra("plane_time"));
				main.setPlaneNumble(data.getStringExtra("plane_numble"));
				main.setEndName(data.getStringExtra("plane_end_address"));
				tv_plane_numble.setText(main.getPlaneNumble());
				tv_plane_time.setText(main.getTime());
				tv_time.setText(main.getTime());
				initView();
			}
			break;
		case MainAttribute.REQUEST_CODE_MESSAGE:
			if (data != null) {
				main.setMessage_PC(data.getStringExtra("message"));
				initView();
			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void OnMenuClick() {
		initView();
	}

	@Override
	public void onPageScrolled(int viewid) {
		if(viewid==Value.VIEWID_PC){
			initView();
		}
	}
}
